//=============================================================================
//Name		:iic.c
//Purpose	:iic communication
//=============================================================================
#define IIC_C
#include "REGtenxTM52F5278B.h"
#include "global.h"
#include "iic.h"
#include <intrins.h>
//=============================================================================
// Function:	void delay_us(unsigned char t)
// Purpose:		delay
// Input:		none
// OutPut:		none
// Author:
//=============================================================================
void delay_us(unsigned char t)	//(6+2.46*(t-1))us
{
	unsigned char i;
	for(i = 0;i < t;i++)
	{
		_nop_();
	}
}
//=============================================================================
#ifdef IIC_COMM

#define IIC_WRITE 0xa0
//=============================================================================
// Function:	void IIC_RW(void)
// Purpose:		IIC read & write communication
// Input:		none
// OutPut:		none
// Author:
//=============================================================================
void IIC_RW(void)
{
	IIC_CS = 0;
	IIC_CLK = 0;
	delay_us(80);
	switch(IIC_ReadOneByte())
	{
		case IIC_WRITE:
			IIC_WriteBytes();
			break;
		default:
			_nop_();
			break;
	}
	IIC_CLK = 1;
	IIC_CS = 1;
}
//=============================================================================
// Function:	unsigned char IIC_ReadOneByte(void)
// Purpose:		IIC read one byte
// Input:		none
// OutPut:		read data
// Author:
//=============================================================================
unsigned char IIC_ReadOneByte(void)
{
	unsigned char l_ucIICReadData,i;
	l_ucIICReadData = 0;
	IIC_SDI = 1;
	for(i = 0;i < 8;i++)	//read 8 bits
	{
		l_ucIICReadData = l_ucIICReadData << 1;
		delay_us(1);
		IIC_CLK = 1;
		delay_us(2);
		if(IIC_SDI == 1)
			l_ucIICReadData |= 0x01;
		IIC_CLK = 0;
		IIC_SDI = 1;
	}
	return l_ucIICReadData;
}
//=============================================================================
// Function:	void IIC_WriteOneByte(unsigned char wdata)
// Purpose:		IIC write one byte
// Input:		write data
// OutPut:		none
// Author:
//=============================================================================
void IIC_WriteOneByte(unsigned char data_w)
{
	unsigned char l_ucIICWriteData,i;
	l_ucIICWriteData = data_w;
	for(i = 0;i < 8;i++)
	{
		if(l_ucIICWriteData & 0x80)
		{
			IIC_SDO = 1;
		}
		else
		{
			IIC_SDO = 0;
		}
		delay_us(1);
		IIC_CLK = 1;
		delay_us(3);
		IIC_CLK = 0;
		l_ucIICWriteData = l_ucIICWriteData << 1;
	}
}
//=============================================================================
// Function:	void IIC_WriteBytes()
// Purpose:		IIC write bytes
// Input:		none
// OutPut:		none
// Author:
//=============================================================================
void IIC_WriteBytes()
{
	unsigned char i;
	unsigned char* l_p_ucTemp;
	l_p_ucTemp = (unsigned char*)&g_wTkStatus;
	IIC_WriteOneByte(*(l_p_ucTemp+1));
	IIC_WriteOneByte(*l_p_ucTemp);
	IIC_WriteOneByte(TKCOUNT);
	l_p_ucTemp = (unsigned char*)&g_a_iTkRawDataArray[0];
	for(i = 0;i < 32;i++)
		IIC_WriteOneByte(*(l_p_ucTemp+i));
	l_p_ucTemp = (unsigned char*)&g_a_iTkOutputArray[0];
	for(i = 0;i < 16;i++)
		IIC_WriteOneByte(*(l_p_ucTemp+i));
}
#endif